<?php
/**
 * Amazon国家（基础）
 * (model)
 */

namespace App\Models\Basics;

use Illuminate\Database\Eloquent\Model;
class AmazonCountry extends Model
{
    protected $table = 'b_amazon_country';

    public $timestamps = false;

    protected $guarded = [];

    public static $_fanEu = [
        1 => '是',
        2 => '否',
        0 => '非欧',
    ];

    public function country()
    {
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    //切换API后删除amazon_area
    public function amazon_area()
    {
        return $this->belongsTo(AmazonArea::class,'area_id');
    }

    public function currency()
    {
        return $this->belongsTo(Currency::class,'currency_code','code');
    }

    public static function getValueByCountryId($countryId,$field){
        return self::query()->where('country_id',$countryId)->value($field);
    }

    public static function getCurrencyCodeByCountryId($countryId){
        return self::query()->where('country_id',$countryId)->value('currency_code');
    }

    public static function getCountryIdByCurrencyCode($currencyCode){
        return self::query()->where('currency_code',$currencyCode)->value('country_id');
    }

    public static function getCountryIdByMarketplaceId($marketplaceId){
        return self::query()->where('marketplace_id',$marketplaceId)->value('country_id');
    }

    public static function getMarketplaceIdByCountryId($countryId){
        return self::query()->where('country_id',$countryId)->value('marketplace_id');
    }

    public static function getVatByCountryId($countryId){
        return self::query()->where('country_id',$countryId)->value('vat');
    }

    public static function marketplaceOptions()
    {
        $re = self::query()->get()->map(function ($model){
            return collect(['marketplace_id' => $model->marketplace_id,
                'country' => $model->country->country ?? '']);
        });
        return $re->pluck('country','marketplace_id');
    }

    public static function options($where = [])
    {
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        $re = $query->get()->map(function ($model){
            return collect(['country_id' => $model->country_id,
                'country' => $model->country->country ?? '']);
        });
        return $re->pluck('country','country_id');
    }
}
